﻿@model AdminUserSetPermissionGetViewModel
@{
    ViewBag.Title = "设置特殊权限";
    string selectClass = "error";
}

<table class="table table-border table-bordered table-hover table-bg">
    <thead>
        <tr>
            <th scope="col" colspan="9">管理员：@Model.User.Name</th>
        </tr>
        <tr class="text-c">
            <th width="40">权限ID</th>
            <th width="150">权限名</th>
            <th>控制器名称</th>
            <th>Action名称</th>
            <th>请求方式</th>
            <th width="180">操作</th>
        </tr>
    </thead>
    <tbody>
        @{
            foreach (var permission in @Model.Permissions)
            {
                AdminUserPermissionDTO ap = Model.HasPermissions.SingleOrDefault(hp => hp.PermissionId == permission.Id);
                <tr class='text-c  trLevevl@(permission.PermissionLevel) @(ap==null?"":selectClass)'>
                    <td>@permission.Id</td>
                    <td class="text-l">
                        @for (int i = 0; i < permission.PermissionLevel; i++)
                        {
                           @:|-
                        }
                        @permission.Name
                    </td>
                    <td>@permission.ControllerName</td>
                    <td>@permission.ActionName</td>
                    <td>@permission.HttpMethod</td>
                    <td class="td-manage">
                        <label for="radio_@(permission.Id)_1">
                            允许<input type="radio" class="radio mr-5" id="radio_@(permission.Id)_1" adminId="@Model.User.Id" permissionId="@permission.Id" value="1" name="radio_@permission.Id" checked="@(ap!=null&&ap.HasPermission==true)">
                        </label>
                        <label for="radio_@(permission.Id)_0">
                            拒绝<input type="radio" class="radio mr-5" id="radio_@(permission.Id)_0" adminId="@Model.User.Id" permissionId="@permission.Id" value="0" name="radio_@permission.Id" checked="@(ap!=null&&ap.HasPermission==false)">
                        </label>
                        <input class="btn btn-danger radius btnDel" type="button" adminId="@Model.User.Id" permissionId="@permission.Id" value="删除">
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

@section FooterScript
{
    <script type="text/javascript">
        $(function () {
            $(":radio").click(function () {
                var $parent = $(this).parents("tr");
                $.ajax({
                    type: 'POST',
                    url: "/Webadmin/AdminUser/SetPermission",
                    data:{ adminId: $(this).attr("adminId"), permissionId: $(this).attr("permissionId"), flg: $(this).val() },
                    success: function (res) {
                        if (res.status == "ok") {
                            parent.layer.msg('设置成功!', { icon: 1, time: 1000 }, function () {
                                $parent.addClass("@selectClass");
                            });
                        } else if (res.status == "redirect") {
                            location.href = res.data;
                        } else if (res.status == "error") {
                            layer.msg(res.errorMsg, { icon: 2, time: 1000 });
                        } else {
                            layer.msg("设置失败", { icon: 2, time: 1000 });
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        layer.msg('网络出错!', { icon: 2, time: 1000 });
                    }
                });
            });
            //删除
            $(".btnDel").click(function () {
                var $parent = $(this).parents("tr");
                $.ajax({
                    type: 'POST',
                    url: "/Webadmin/AdminUser/SetPermissionDel",
                    data: { adminId: $(this).attr("adminId"), permissionId: $(this).attr("permissionId") },
                    success: function (res) {
                        if (res.status == "ok") {
                            parent.layer.msg('删除成功!', { icon: 1, time: 1000 }, function () {
                                //去除背景颜色，取消选中
                                $parent.removeClass("@selectClass").find(":radio").prop("checked", "");
                            });
                        } else if (res.status == "redirect") {
                            location.href = res.data;
                        } else if (res.status == "error") {
                            layer.msg(res.errorMsg, { icon: 2, time: 1000 });
                        } else {
                            layer.msg("删除失败", { icon: 2, time: 1000 });
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        layer.msg('网络出错!', { icon: 2, time: 1000 });
                    }
                });
            });
        });
    </script>
}


